﻿namespace MyManagement.Model
{
    using System;
    using System.Collections.Generic;    
    using System.Linq;

    /// <summary>
    /// Holds the backlog entries.
    /// </summary>
    public class Backlog
    {
        private List<BacklogItem> items;

        /// <summary>
        /// Initializes a new instance of the <see cref="Backlog" /> class.
        /// </summary>
        internal Backlog()
        {
            this.items = new List<BacklogItem>();
        }

        /// <summary>
        /// Gets the backlog items.
        /// </summary>
        public IEnumerable<BacklogItem> Items
        {
            get
            {
                return this.items;
            }
        }

        /// <summary>
        /// Gets the product to use by default.
        /// </summary>
        public Product DefaultProduct
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the project to use by default.
        /// </summary>
        public Project DefaultProject
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the category to use by default.
        /// </summary>
        public BacklogItemCategory DefaultCategory
        {
            get;
            private set;
        }

        /// <summary>
        /// Adds the given item to the backlog.
        /// </summary>
        /// <param name="item">The item to add to the backlog.</param>
        public void AddItem(BacklogItem item)
        {
            // validate input

            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            // set the parent before anything else because it is required internally

            item.ChangeBacklog(this);

            // make sure the object is according to the business rules

            item.EnsureValid();

            // apply the default values

            item.ApplyDefaultValues();

            // add it to the collection

            this.items.Add(item);
        }

        /// <summary>
        /// Changes the default product.
        /// </summary>
        /// <param name="value">The default product.</param>
        public void ChangeDefaultProduct(Product value)
        {
            // validate input
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            // set the default

            this.DefaultProduct = value;
        }

        /// <summary>
        /// Changes the default project.
        /// </summary>
        /// <param name="value">The value.</param>
        public void ChangeDefaultProject(Project value)
        {
            // validate input
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            // set the default

            this.DefaultProject = value;
        }

        /// <summary>
        /// Changes the default category.
        /// </summary>
        /// <param name="value">The value.</param>
        public void ChangeDefaultCategory(BacklogItemCategory value)
        {
            // validate input
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            // set the default

            this.DefaultCategory = value;
        }
    }
}